package com.xqboss.web.controller.chain;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.dto.IdDto;
import com.xqboss.apps.service.nft.NftNumberService;
import com.xqboss.apps.util.mh.BeanCopierUtil;
import com.xqboss.chain.domain.chain.UpUpdateChain;
import com.xqboss.chain.domain.chain.UpUpdateChainLog;
import com.xqboss.chain.domain.chain.dto.GetUpChainListDto;
import com.xqboss.chain.domain.chain.dto.GetUpChainLogListDto;
import com.xqboss.chain.service.EChainService;
import com.xqboss.chain.service.UpUpdateChainLogService;
import com.xqboss.chain.service.UpUpdateChainService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 藏品、装备Controller
 *
 * @author xqboss
 * @date 2023-08-22
 */
@Api(tags = {"上链管理"})
@RestController
@RequestMapping("/web/chain")
public class ChainController extends BaseController {

    @Autowired
    private UpUpdateChainService upUpdateChainService;

    @Autowired
    private UpUpdateChainLogService upUpdateChainLogService;

    @Autowired
    private NftNumberService nftNumberService;

    @Autowired
    private EChainService eChainService;

    @PreAuthorize("@ss.hasPermi('web:chain:up-chain-list')")
    @ApiOperation("获取上链队列列表")
    @GetMapping("up-chain-list")
    public R<List<UpUpdateChainListVo>> upChainList(@Validated GetUpChainListDto params) {
        params.startPageAndOrder();
        List<UpUpdateChain> list = upUpdateChainService.list(params);
        List<UpUpdateChainListVo> voList = BeanCopierUtil.copylist(list, UpUpdateChainListVo.class);
        for (UpUpdateChainListVo vo : voList) {
            // 获取藏品信息
            vo.setNftInfo(nftNumberService.selectNftInfo(vo.getNftNumberId()));
        }
        return R.ok(voList, list);
    }

    @PreAuthorize("@ss.hasPermi('web:chain:up-chain-log')")
    @ApiOperation("获取上链记录")
    @GetMapping("up-chain-log")
    public R<List<UpUpdateChainLog>> upChainLogList(@Validated GetUpChainLogListDto params) {
        params.startPageAndOrder();
        List<UpUpdateChainLog> list = upUpdateChainLogService.list(new LambdaQueryWrapper<>(params));
        return R.ok(list);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:chain:up-chain')")
    @ApiOperation("执行上链")
    @PutMapping("up-chain")
    @Log(title = "上链", businessType = BusinessType.UPDATE)
    public R upChain(@Validated @RequestBody IdDto params) {
        upUpdateChainService.upchain(params.getId());
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:chain:sync-chain')")
    @ApiOperation("手动同步链上状态")
    @PutMapping("sync-chain")
    @Log(title = "上链", businessType = BusinessType.UPDATE)
    public R syncChain(@Validated @RequestBody IdDto params) {
        upUpdateChainService.syncTx(params.getId());
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:chain:deploy-contract')")
    @ApiOperation("部署ERC721合约")
    @PostMapping("deploy-contract")
    @Log(title = "上链", businessType = BusinessType.UPDATE)
    public R deployContract(@Validated @RequestBody Object params) {
        String contract = eChainService.createContract();
        return R.ok(contract);
    }
}
